package mv.phone.com.phonemv;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
 * Created by Administrator on 2017/4/15.
 */

public class PhoneView extends View {

    private Drawable line1;
    private Drawable line2;
    private Drawable line3;
    private Drawable line4;
    private Drawable line5;
    private Drawable line6;

    private Drawable head;
    private Drawable name;
    private Drawable phone;

    private Drawable ball1;
    private Drawable ball2;
    private Drawable ball3;
    private Drawable ball4;
    private Drawable ball5;
    private Drawable ball6;

    private Drawable light1;
    private Rect light1Rect = new Rect();

    private Drawable light2;
    private Rect light2Rect = new Rect();

    private Drawable light3;
    private Rect light3Rect = new Rect();

    private Drawable light4;
    private Rect light4Rect = new Rect();

    private Drawable light5;
    private Rect light5Rect = new Rect();

    private Drawable light6;
    private Rect light6Rect = new Rect();

    private Drawable open;
    private Drawable close;

    public PhoneView(Context context) {
        super(context);
    }

    public PhoneView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PhoneView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }

    private void init() {
        head = getResources().getDrawable(R.drawable.head);
        name = getResources().getDrawable(R.drawable.name);
        phone = getResources().getDrawable(R.drawable.phone);

        line1 = getResources().getDrawable(R.drawable.line_1);
        line2 = getResources().getDrawable(R.drawable.line_2);
        line3 = getResources().getDrawable(R.drawable.line_3);
        line4 = getResources().getDrawable(R.drawable.line_4);
        line5 = getResources().getDrawable(R.drawable.line_5);
        line6 = getResources().getDrawable(R.drawable.line_6);

        ball1 = getResources().getDrawable(R.drawable.ball_1);
        ball2 = getResources().getDrawable(R.drawable.ball_2);
        ball3 = getResources().getDrawable(R.drawable.ball_3);
        ball4 = getResources().getDrawable(R.drawable.ball_4);
        ball5 = getResources().getDrawable(R.drawable.ball_5);
        ball6 = getResources().getDrawable(R.drawable.ball_6);

        light1 = getResources().getDrawable(R.drawable.light_1);
        light2 = getResources().getDrawable(R.drawable.light_2);
        light3 = getResources().getDrawable(R.drawable.light_3);
        light4 = getResources().getDrawable(R.drawable.light_4);
        light5 = getResources().getDrawable(R.drawable.light_5);
        light6 = getResources().getDrawable(R.drawable.light_6);

        open = getResources().getDrawable(R.drawable.open);
        close = getResources().getDrawable(R.drawable.close);

    }

    @Override
    protected void onLayout(boolean c, int l, int t, int r, int b) {
        int w = r - l;
        int h  = b - t;
        int line12W = (int) (w / 2f);
        int line12h = (int) (h / 2.1f);
        line1.setBounds(0, 0, line12W, line12h);
        line2.setBounds(w - line12W, 0, w, line12h);

        int line34W = line12W;
        int line34H = h * 1 / 7;
        int line34T = h / 2 - line34H / 2;

        line3.setBounds(0, line34T, line34W - w / 20, line34T + line34H);
        line4.setBounds(w - line34W, line34T - h / 90, w, line34T + line34H - h / 90);

        line5.setBounds(0, h - line12h, line12W, h);
        line6.setBounds(w - line12W, h - line12h, w, h);

        int light1W = w / 17;
        int light1H = light1W * ((BitmapDrawable) light1).getBitmap().getHeight() / ((BitmapDrawable) light1).getBitmap().getWidth();
        int light1L = w / 2 - w / 4;
        int light1T = (int) (h / 2 - light1H - h / 4.9);
        light1Rect.set(light1L, light1T, light1L + light1W, light1T + light1H);
        light1.setBounds(light1Rect);

        int light2W = w / 17;
        int light2H = light2W * ((BitmapDrawable) light2).getBitmap().getHeight() / ((BitmapDrawable) light2).getBitmap().getWidth();
        int light2L = w / 2 + w / 4 - light2W;
        int light2T = (int) (h / 2 - light2H - h / 4.9);
        light2Rect.set(light2L, light2T, light2L + light2W, light2T + light2H);
        light2.setBounds(light2Rect);

        int light3W = w / 100;
        int light3H = light3W * ((BitmapDrawable) light3).getBitmap().getHeight() / ((BitmapDrawable) light3).getBitmap().getWidth();
        int light3L = w / 2 - w / 5;
        int light3T = (int) (h / 2 - light3H / 2);
        light3Rect.set(light3L, light3T, light3L + light3W, light3T + light3H);
        light3.setBounds(light3Rect);

        int light4W = w / 100;
        int light4H = light4W * ((BitmapDrawable) light4).getBitmap().getHeight() / ((BitmapDrawable) light4).getBitmap().getWidth();
        int light4L = w / 2 + w / 5 - light4W;
        int light4T = (int) (h / 2 - light4H / 2);
        light4Rect.set(light4L, light4T, light4L + light4W, light4T + light4H);
        light4.setBounds(light4Rect);

        int light5W = w / 17;
        int light5H = light5W * ((BitmapDrawable) light5).getBitmap().getHeight() / ((BitmapDrawable) light5).getBitmap().getWidth();
        int light5L = (int) (w / 2 - w / 5.1f);
        int light5T = (int) (h / 2 + h / 6.9);
        light5Rect.set(light5L, light5T, light5L + light5W, light5T + light5H);
        light5.setBounds(light5Rect);

        int light6W = w / 17;
        int light6H = light6W * ((BitmapDrawable) light6).getBitmap().getHeight() / ((BitmapDrawable) light6).getBitmap().getWidth();
        int light6L = (int) (w / 2 + w / 5.1f - light6W);
        int light6T = (int) (h / 2 + h / 6.9);
        light6Rect.set(light6L, light6T, light6L + light6W, light6T + light6H);
        light6.setBounds(light6Rect);

        int ball1W = (int) (w / 2);
        int ball1H = ball1W * ((BitmapDrawable) ball1).getBitmap().getHeight() / ((BitmapDrawable) ball1).getBitmap().getWidth();
        int ball1L = (int) (-ball1W / 2.1f);
        int ball1T = h / 2 - ball1H - h / 9;
        ball1.setBounds(ball1L, ball1T, ball1L + ball1W, ball1T + ball1H);

        int ball2W = (int) (w / 2);
        int ball2H = ball2W * ((BitmapDrawable) ball2).getBitmap().getHeight() / ((BitmapDrawable) ball2).getBitmap().getWidth();
        int ball2L = (int) (w - ball2W + ball2W / 2.2f);
        int ball2T = h / 2 - ball2H - h / 10;
        ball2.setBounds(ball2L, ball2T, ball2L + ball2W, ball2T + ball2H);

        int ball3W = (int) (w / 2);
        int ball3H = ball3W * ((BitmapDrawable) ball3).getBitmap().getHeight() / ((BitmapDrawable) ball3).getBitmap().getWidth();
        int ball3L = (int) (-ball3W / 2.2f);
        int ball3T = h / 2 + h / 15;
        ball3.setBounds(ball3L, ball3T, ball3L + ball3W, ball3T + ball3H);

        int ball4W = (int) (w / 2);
        int ball4H = ball4W * ((BitmapDrawable) ball4).getBitmap().getHeight() / ((BitmapDrawable) ball4).getBitmap().getWidth();
        int ball4L = (int) (w - ball4W + ball4W / 2.2f);
        int ball4T = h / 2 + h / 15;
        ball4.setBounds(ball4L, ball4T, ball4L + ball4W, ball4T + ball4H);

        int ball5W = w / 10;
        int ball5H = ball5W * ((BitmapDrawable) ball5).getBitmap().getHeight() / ((BitmapDrawable) ball5).getBitmap().getWidth();
        int ball5L = (w - ball5W) / 2;
        int ball5T = (int) (h / 2 + h / 4.5);
        ball5.setBounds(ball5L, ball5T, ball5L + ball5W, ball5T + ball5H);

        int ball6W = w / 20;
        int ball6H = ball6W * ((BitmapDrawable) ball6).getBitmap().getHeight() / ((BitmapDrawable) ball6).getBitmap().getWidth();
        int ball6L = (w - ball6W) / 2;
        int ball6T = (int) (h / 20);
        ball6.setBounds(ball6L, ball6T, ball6L + ball6W, ball6T + ball6H);

        int nameW = w / 5;
        int nameH = nameW * ((BitmapDrawable) name).getBitmap().getHeight() / ((BitmapDrawable) name).getBitmap().getWidth();
        int nameL = (w  - nameW) / 2;
        int nameT = (h / 2 - h / 120 - nameH);
        name.setBounds(nameL, nameT, nameL + nameW, nameT + nameH);

        int phoneW = (int) (w / 2f);
        int phoneH = phoneW * ((BitmapDrawable) phone).getBitmap().getHeight() / ((BitmapDrawable) phone).getBitmap().getWidth();
        int phoneL = (w  - phoneW) / 2;
        int phoneT = (h / 2 + h / 60);
        phone.setBounds(phoneL, phoneT, phoneL + phoneW, phoneT + phoneH);

        int headW = (int) (w / 2.5f);
        int headH = headW;
        int headL = (w - headW) / 2;
        int headT = name.getBounds().top - h / 25 - headH;
        head.setBounds(headL, headT, headL + headW, headT + headH);

        int closeW = (int) (w / 4.5f);
        int closeH = closeW;
        int closeL = w / 2 - w / 6 - closeW;
        int closeT = h - h / 30 - closeH;
        close.setBounds(closeL, closeT, closeL + closeW, closeT + closeH);

        int openW = (int) (w / 4.5f);
        int openH = closeW;
        int openL = w / 2 + w / 6;
        int openT = h - h / 30 - openH;
        open.setBounds(openL, openT, openL + openW, openT + openH);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.scale(scale, scale, getWidth() / 2, getHeight() / 2);
        line1.setAlpha(alphaLine);
        line2.setAlpha(alphaLine);
        line3.setAlpha(alphaLine);
        line4.setAlpha(alphaLine);
        line5.setAlpha(alphaLine);
        line6.setAlpha(alphaLine);

        ball1.setAlpha(alphaLight);
        ball2.setAlpha(alphaLight);
        ball3.setAlpha(alphaLight);
        ball4.setAlpha(alphaLight);
        ball5.setAlpha(alphaLight);
        ball6.setAlpha(alphaLight);

        line1.draw(canvas);
        line2.draw(canvas);
        line3.draw(canvas);
        line4.draw(canvas);
        line5.draw(canvas);
        line6.draw(canvas);

        ball1.draw(canvas);
        ball2.draw(canvas);
        ball3.draw(canvas);
        ball4.draw(canvas);
        ball5.draw(canvas);
        ball6.draw(canvas);

        for (int i = 0; i < 15; i++) {
            float addleft = -light1Rect.width() * 0.7f;
            int leftAdd = (int) (addleft * i);
            float addtop = -light1Rect.height() * 1.5f * 0.7f;
            int topAdd = (int) (addtop * i);
            int widthAdd = (int) (light1Rect.width() * i * 0.7f / 2);

            int width = light1Rect.width() + widthAdd;
            int height = width * light1Rect.height() / light1Rect.width();
            int left = light1Rect.left + leftAdd;
            int right = left + width;
            int top = light1Rect.top + topAdd;
            int bottom = top + height;

            light1.setBounds(left, top, right, bottom);
            canvas.save();
            canvas.translate(addleft * 4 * scalelight, addtop  * 4 * scalelight);
            light1.draw(canvas);
            canvas.restore();
        }

        for (int i = 0; i < 15; i++) {
            float addleft = light2Rect.width() * 0.4f;
            int leftAdd = (int) (addleft * i);
            float addtop = -light2Rect.height() * 1.5f * 0.7f;
            int topAdd = (int) (addtop * i);
            int widthAdd = (int) (light2Rect.width() * i * 0.7f / 2);

            int width = light2Rect.width() + widthAdd;
            int height = width * light2Rect.height() / light2Rect.width();
            int left = light2Rect.left + leftAdd;
            int right = left + width;
            int top = light2Rect.top + topAdd;
            int bottom = top + height;

            light2.setBounds(left, top, right, bottom);
            canvas.save();
            canvas.translate(addleft * 4 * scalelight, addtop  * 3 * scalelight);
            light2.draw(canvas);
            canvas.restore();
        }

        for (int i = 0; i < 13; i++) {
            int addleft = -light3Rect.height();
            int leftAdd = (int) (addleft * i);
            int addtop = -light3Rect.height();
            int topAdd = (int) (addtop * 0.25f * i);
            int widthAdd = (int) (light3Rect.width() * i / 2);

            int width = light3Rect.width() + widthAdd;
            int height = width * light3Rect.height() / light3Rect.width();
            int left = light3Rect.left + leftAdd;
            int right = left + width;
            int top = light3Rect.top + topAdd;
            int bottom = top + height;

            light3.setBounds(left, top, right, bottom);
            canvas.save();
            canvas.translate(addleft * 4 * scalelight, addtop  * 0 * scalelight);
            light3.draw(canvas);
            canvas.restore();
        }

        for (int i = 0; i < 13; i++) {
            float addleft = light4Rect.height() * 1.4f;
            int leftAdd = (int) (addleft * i);
            float addtop = -light4Rect.height() * 0.4f * 1.4f;
            int topAdd = (int) (addtop * i);
            int widthAdd = (int) (light4Rect.width() * 1.8f * i / 2);

            int width = light4Rect.width() + widthAdd;
            int height = width * light4Rect.height() / light4Rect.width();
            int left = light4Rect.left + leftAdd;
            int right = left + width;
            int top = light4Rect.top + topAdd;
            int bottom = top + height;

            light4.setBounds(left, top, right, bottom);
            canvas.save();
            canvas.translate(addleft * 4 * scalelight, addtop  * 0 * scalelight);
            light4.draw(canvas);
            canvas.restore();
        }

        for (int i = 0; i < 15; i++) {
            float addleft = -light5Rect.width() * 0.9f;
            int leftAdd = (int) (addleft * i);
            float addtop = light5Rect.height() * 1.5f * 0.9f;
            int topAdd = (int) (addtop * i);
            int widthAdd = (int) (light5Rect.width() * i * 0.9f / 2);

            int width = light5Rect.width() + widthAdd;
            int height = width * light5Rect.height() / light5Rect.width();
            int left = light5Rect.left + leftAdd;
            int right = left + width;
            int top = light5Rect.top + topAdd;
            int bottom = top + height;

            light5.setBounds(left, top, right, bottom);
            canvas.save();
            canvas.translate(addleft * 3 * scalelight, addtop  * 4 * scalelight);
            light5.draw(canvas);
            canvas.restore();
        }

        for (int i = 0; i < 15; i++) {
            float addleft = light6Rect.width() * 0.4f * 0.9f;
            int leftAdd = (int) (addleft * i);
            float addtop = light6Rect.height() * 1.5f * 0.9f;
            int topAdd = (int) (addtop * i);
            int widthAdd = (int) (light6Rect.width() * i * 0.9f / 2);

            int width = light6Rect.width() + widthAdd;
            int height = width * light6Rect.height() / light6Rect.width();
            int left = light6Rect.left + leftAdd;
            int right = left + width;
            int top = light6Rect.top + topAdd;
            int bottom = top + height;

            light6.setBounds(left, top, right, bottom);
            canvas.save();
            canvas.translate(addleft * 4 * scalelight, addtop  * 3 * scalelight);
            light6.draw(canvas);
            canvas.restore();
        }

        canvas.restore();

        head.draw(canvas);
        name.draw(canvas);
        phone.draw(canvas);

        close.draw(canvas);

        canvas.save();
        canvas.translate(0, phoneY);
        open.draw(canvas);
        canvas.restore();
    }

    final int STYLE_1 = 0;
    final int STYLE_2 = 1;
    int style = STYLE_1;
    int startCount = 0;
    int alphaLine = 0;
    int alphaLight = 255;
    float scale = 0.7f;
    int phoneY = 0;

    float scalelight = 1;

    public void startAnimation() {
        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if ( style == STYLE_1) {
                    alphaLine = 255;
                    alphaLight = 255;
                    scale = 0.7f + (1 - 0.7f) * interpolatedTime;
                } else {
                    alphaLine = (int) (255 * interpolatedTime);
                    alphaLight = 255 - (int) (255 * interpolatedTime);
                    scale = 1;
                }
                if (interpolatedTime < 0.5) {
                    scalelight =  1 * (interpolatedTime / 0.5f);
                    phoneY = -(int) ((getWidth() / 9) * (interpolatedTime / 0.5f));
                } else {
                    phoneY = -(int) ((getWidth() / 9) * ((interpolatedTime - 0.5f) / 0.5f));
                    scalelight =  1 * ((interpolatedTime - 0.5f) / 0.5f);
                }
//                scalelight =  (1) * interpolatedTime;
                invalidate();
            }
        };
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                if (startCount % 9 == 0) {
                    style = STYLE_1;
                    if (startCount != 0) {
                        startCount = 0;
                    }
                } else {
                    style = STYLE_2;
                }

                startCount++;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                if (startCount % 9 == 0) {
                    style = STYLE_1;
                    if (startCount != 0) {
                        startCount = 0;
                    }
                } else {
                    style = STYLE_2;
                }

                startCount++;
            }
        });
        animation.setDuration(500);
        animation.setRepeatCount(Animation.INFINITE);
        animation.setRepeatMode(Animation.RESTART);
        startAnimation(animation);
    }
}
